﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace MyDelegate2
{
    /// <summary>
    /// Summary description for MyDelegete2
    /// </summary>
    public class MyDelegete2
    {
        public MyDelegete2()
        {
            //
            // TODO: Add constructor logic here
            //
        }
    }

    public class Heater
    {
        private int temperature;
        public string type = "RealFire 001";       // 添加型号作为演示
        public string area = "China Xian";         // 添加产地作为演示
        //声明委托
        public delegate string BoiledEventHandler(Object sender, BoiledEventArgs e);
        public event BoiledEventHandler Boiled; //声明事件

        // 定义BoiledEventArgs类，传递给Observer所感兴趣的信息
        public class BoiledEventArgs : EventArgs
        {
            public readonly int temperature;
            public BoiledEventArgs(int temperature)
            {
                this.temperature = temperature;
            }
        }

        // 可以供继承自 Heater 的类重写，以便继承类拒绝其他对象对它的监视
        protected virtual string OnBoiled(BoiledEventArgs e)
        {
            string strTemp = string.Empty;
            if (Boiled != null)
            { // 如果有对象注册
                strTemp = Boiled(this, e);  // 调用所有注册对象的方法
            }

            return strTemp;
        }

        // 烧水。
        public string BoilWater()
        {
            string strTemp = string.Empty;
            for (int i = 0; i <= 100; i++)
            {
                temperature = i;
                if (temperature > 95)
                {
                    //建立BoiledEventArgs 对象。
                    BoiledEventArgs e = new BoiledEventArgs(temperature);
                    strTemp += OnBoiled(e) + "<br>";  // 调用 OnBolied方法
                }
            }

            return strTemp;
        }
    }

    // 警报器
    public class Alarm
    {
        public string MakeAlert(Object sender, Heater.BoiledEventArgs e)
        {
            Heater heater = (Heater)sender;     //这里是不是很熟悉呢？
            //访问 sender 中的公共字段
            string str = string.Empty;
            str += string.Format("Alarm：{0} - {1}: ", heater.area, heater.type);
            str += string.Format("Alarm: 嘀嘀嘀，水已经 {0} 度了：", e.temperature);
            str += "<br>";

            return str;
        }
    }

    // 显示器
    public class Display
    {
        public static string ShowMsg(Object sender, Heater.BoiledEventArgs e)
        {   //静态方法
            Heater heater = (Heater)sender;

            string str = string.Empty;
            str += string.Format("Display：{0} - {1}: ", heater.area, heater.type);
            str += string.Format("Display：水快烧开了，当前温度：{0}度。", e.temperature);
            str += "<br>";

            return str;
        }
    }

    //class Program
    //{
    //    static void Main()
    //    {
    //        Heater heater = new Heater();
    //        Alarm alarm = new Alarm();

    //        heater.Boiled += alarm.MakeAlert;   //注册方法
    //        heater.Boiled += (new Alarm()).MakeAlert;      //给匿名对象注册方法
    //        heater.Boiled += new Heater.BoiledEventHandler(alarm.MakeAlert);    //也可以这么注册
    //        heater.Boiled += Display.ShowMsg;       //注册静态方法

    //        heater.BoilWater();   //烧水，会自动调用注册过对象的方法
    //    }
    //}

}